#!/bin/sh

PRODUCTNAME="${1}"
PLISTNAME="${2}"
APPFILENAME="${3}"
BASEMINVERSIONNUMBER="${4}"
echo "Product Name: ${PRODUCTNAME}"
echo "Plist Name: ${PLISTNAME}"
echo "Application File Name: ${APPFILENAME}"
echo "Base Product Min Version Number: ${BASEMINVERSIONNUMBER}"

SCRIPTDIR=$(dirname "${0}")
SCRIPTDIR=$(dirname "${SCRIPTDIR}")
SCRIPTDIR=$(dirname "${SCRIPTDIR}")
echo "Script Path : ${0}"
echo "Script Folder: ${SCRIPTDIR}"

PACKAGEDIR="${SCRIPTDIR}"
echo "PackageDir: ${PACKAGEDIR}"

#Version_A to be installed.
if [ -z "${BASEMINVERSIONNUMBER}" ]; then
	VERSION_A=`defaults read "${PACKAGEDIR}/Contents/Packages/${PRODUCTNAME} Application.pkg/Contents/Info" "CFBundleShortVersionString"`
	if [ -z "${VERSION_A}" ]; then 
		VERSION_A=`defaults read "${PACKAGEDIR}/Contents/Packages/${PRODUCTNAME} Application Update.pkg/Contents/Info" "CFBundleShortVersionString"`
	fi
else
	VERSION_A="${BASEMINVERSIONNUMBER}"
fi
echo "Version A: ${VERSION_A}"

#Reading String VERSION_A to write it into Var 1-4 without "."
A1=`expr "${VERSION_A}" : '\([0-9]*\)'`
A2=`expr "${VERSION_A}" : '[0-9]*\.\([0-9]*\)'`
A3=`expr "${VERSION_A}" : '[0-9]*\.[0-9]*\.\([0-9]*\)'`
A4=`expr "${VERSION_A}" : '[0-9]*\.[0-9]*\.[0-9]*[\.(RC ]*\([0-9]*\)'`

#echo "Source $A1 $A2 $A3 $A4" > /tmp/installerversioncheck.txt

#Version_B located on the system. If there is none, product will be installed to default location.
#Does Plist exist?
if [ -e "/Library/Preferences/${PLISTNAME}.plist" ]; then
	SCRIPTDIR=$(dirname "${0}")
	SCRIPTDIR=$(dirname "${SCRIPTDIR}")
 	InstallDir=$("${SCRIPTDIR}/Resources/pathhelper" read "/Library/Preferences/${PLISTNAME}" "InstallDir")
 	echo InstallDir: "${InstallDir}"
 	#Does InstallDir exist?
 	if [ -e "${InstallDir}/${APPFILENAME}.app" ]; then
 		#-> Version number B set here
 			VERSION_B=`defaults read "${InstallDir}/${APPFILENAME}.app/Contents/Info" "CFBundleShortVersionString"`
 			echo Version B: "Version B: ${VERSION_B}"
 			
 			#Reading String VERSION_A to write it into Var 1-4 without "."
 			B1=`expr "${VERSION_B}" : '\([0-9]*\)'`
 			B2=`expr "${VERSION_B}" : '[0-9]*\.\([0-9]*\)'`
 			B3=`expr "${VERSION_B}" : '[0-9]*\.[0-9]*\.\([0-9]*\)'`
 			B4=`expr "${VERSION_B}" : '[0-9]*\.[0-9]*\.[0-9]*[\.(RC ]*\([0-9]*\)'`
 			
			#echo "Destination $B1 $B2 $B3 $B4" >> /tmp/installerversioncheck.txt
 	else
 		echo "InstallDir or application does not exist. Cancel install in case of SC update installer. Proceed with install in case of product full installer."
 		exit 3
 	fi
 else
 	echo "Plist does not exist. Cancel install in case of product update installer. Proceed with install in case of product full installer."
 	exit 3
 fi

#Create Arrays for each Version
array_A=( "${A1}" "${A2}" "${A3}" "${A4}" )
array_B=( "${B1}" "${B2}" "${B3}" "${B4}" )

Nr="0"
while [ ${Nr} -lt 4 ]; do
	if [ ${array_A[${Nr}]} -gt ${array_B[${Nr}]} ]
	then 
		echo "Version A > Version B. Version_A will be installed in any case."
		exit 0
	fi
	if [ ${array_A[${Nr}]} -lt ${array_B[${Nr}]} ]
	then 
		echo "Version A < Version B. Cancel install in any case."
		exit 1
	fi
	let 'Nr=Nr+1'
done

# If version numbers are equal - script exits with 2
echo "Version A = Version B. Cancel install in case of embedded product installer. Proceed with install in case of main product installer. Decide in Java code."
exit 2